import { _decorator, Button,sys,Animation, Component, EventTarget, find, Label, Node } from 'cc';
import { EnemyManager } from './EnemyManager';
const { ccclass, property } = _decorator;

export enum GameState{
    GAME_OVER = "GAME_OVER", // 游戏结束
    START = "START",   // 游戏开始
    WIN = "WIN", // 玩家胜利
    LOSE = "LOSE" // 玩家失败

}
@ccclass('GameManager')
export class GameManager extends Component {

    private static instance: GameManager = null;
    public static getInstance(): GameManager {
        return this.instance;
    }

    @property
    gameState:GameState=GameState.GAME_OVER;

    //资源相关数据
    @property
    private bombNumber:number = 0;      //可以使用的清屏效果数量
    private totalGoldCount:number=0;     //总金币数量
    private goldCount:number=0;     //单局游戏获得的金币数量

    
    // 计时器相关变量
    @property
    private countdownTime: number = 10; // 倒计时初始时间（秒）
    private remainingTime: number = 0;  // 剩余时间
    private isTimerRunning: boolean = false; // 判断计时器是否在运行
    @property(Label)
    private timerIntervalLabel:Label=null

    @property(Animation)
    private anim:Animation=null;
    @property(Animation)
    private goldAddAnim:Animation=null;

    @property(Node)
    private GameOverUI:Node=null;
    

    // protected onLoad(): void {
    //     this.getTotalGoldCount();
    //     // console.log(this.gameState);
    // }

    start() {
        GameManager.instance=this;
        
    }

    update(deltaTime: number) {
        // 如果计时器正在运行，更新时间
        if (this.isTimerRunning) {
            this.remainingTime -= deltaTime;

            // 如果倒计时结束，停止计时器
            if (this.remainingTime <= 0) {
                this.remainingTime = 0;
                this.stopTimer();
                this.gameState=GameState.WIN;
                this.totalGoldCount+=this.goldCount;

                this.setTotalGoldCount();
                this.GameOverUI.active=true;
                // sys.localStorage.setItem("GoldCount",this.totalGoldCount.toString());
                // console.log("setItemISOK!!!");

            }

            // 更新倒计时显示
            this.timerIntervalLabel.string = this.remainingTime.toFixed(2); // 保留两位小数
        }
        
    }
    
    setTotalGoldCount(){
        const gold = JSON.stringify(this.totalGoldCount);

        sys.localStorage.setItem("GoldCount",this.totalGoldCount.toString());
        console.log("setItemISOK!!!","this.totalGoldCount:",this.totalGoldCount,"this.goldCount:",this.goldCount);
    }

    getTotalGoldCount(){
        let newTotalGoldCount = sys.localStorage.getItem("GoldCount");
        if(newTotalGoldCount!==null){
            this.totalGoldCount=parseInt(newTotalGoldCount,10);
            console.log("newTotalGoldCount:",newTotalGoldCount);
        }else{
            console.log("err");
        }
        
    }


    
    GameGAME_OVER(){
        if(this.gameState===GameState.GAME_OVER)return;
        this.gameState=GameState.GAME_OVER;
        this.getTotalGoldCount();

    }
    GameSTART(){
        if(this.gameState===GameState.START)return;
        this.gameState=GameState.START;
        this.resetTimer(); // 重置计时器
        this.startTimer(); // 开始计时器
        EnemyManager.getInstance().initEnemy();
        this.anim.play("HomeBG");
        this.getTotalGoldCount();
        
    }
    GameWIN(){
        if(this.gameState===GameState.WIN)return;
        this.gameState=GameState.WIN;
        this.stopTimer(); // 停止计时器
        this.totalGoldCount+=this.goldCount;

        console.log(this.totalGoldCount);

        EnemyManager.getInstance().onDestroy();
        EnemyManager.getInstance().onDoubleClick();
    }
    GameLOSE(){
        if(this.gameState===GameState.LOSE)return;
        this.gameState=GameState.LOSE;
        this.stopTimer(); // 停止计时器
        EnemyManager.getInstance().killEnemyArray();
        
    }




    // 启动计时器
    startTimer() {
        if (!this.isTimerRunning) {
            this.remainingTime = this.countdownTime; // 初始化剩余时间
            this.isTimerRunning = true;
        }
    }

    // 停止计时器
    stopTimer() {
        if (this.isTimerRunning) {
            this.isTimerRunning = false;
        }
    }
    // 重置计时器
    resetTimer() {
        this.remainingTime = this.countdownTime;
        this.stopTimer();
    }

    // 获取当前游戏时间（秒）
    getGameTime(): number {
        return this.remainingTime;
    }




    isHaveBomb():boolean{
        return this.bombNumber>0;
    }

    useBomb(){
        this.bombNumber-=1;
        // this.bombUI.updateUI(this.bombNumber);
    }
    
    addGold(amount:number){
        this.goldCount+=amount;
        this.goldAddAnim.play("GoldCountAdd");
    }
    subtractGold(amount:number){
        if(amount>this.goldCount){
            this.goldCount=0
        }else{
            this.goldCount-=amount;
        }
    }
    getGoldCount(){
        return this.goldCount;
    }
    
}

